<?php
import('controller.class.php');
import('view.login.class.php');
import('model.user.class.php');
class Login extends Controller{
	var $view = null;
	function __construct(){
		$this->view = new ViewLogin();
	}
	
	public function login(){
		//检查是否存在uuid
		if(!hasArg('uuid')){
			parent::displayByCode(LOGIN_F_NEED_UUID);
			return;
		}
		
		$md5_uuid=getArgs('uuid');
		$md5_uuid=strtoupper($md5_uuid);
		if(!isMd5Format($md5_uuid)){//检查UUID是否合法
			parent::displayByCode(LOGIN_F_UNKOWN_UUID);
			return;
		}
		
		$user = new ModelUser($md5_uuid);
		
		if($user->isUUIDExist()){
			log_d("Found uuid $md5_uuid is exist and then update the last login time." );
			$opr = $user->updateLoginTime();
		}else{
			log_d("Found uuid $md5_uuid is not exist, and then add a new to database." );
			$opr = $user->addNewUser();
		}
		
		if($opr){
			$_SESSION['uuid']=$md5_uuid;
			$_SESSION['login']=TRUE;
			parent::displayByCode(LOGIN_S_OK);
		}else{
			parent::displayByCode(LOGIN_F_UNKOWN_RESON);//登录失败，未知错误
			
		}
	}
	
	public function logout(){
		$_SESSION['login']=false;
	}
	
	public function need_login(){
		parent::displayByCode(LOGIN_F_NEED_LOGIN);
	}
	
	public static function isLogin(){
		if(isset($_SESSION['uuid']) && true == $_SESSION['login']){
			return true;
		}else{
			return false;
		}
	}
}